var app = getApp();
Page(Object.assign({},{
    data:{
        page : {
            isend : false,
            doing : false,
            doo : 'admin',
            pdata : {
                op : 'cardlist',
                page : 1,
                type : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        tab : {
            color : '#449aff',
            list: [{
            id: '0',
                title: '在线使用',
            }, {
            id: '1',
                title: '到店使用',
            }],
            selectedId: '0',
            scroll: false
        },
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        
        app.com.auth(function(user){
            
            app.com.getPage(self,self.data.page,0,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   params : data.params,
                   inited : true,
                });
            },false,function(res){
                app.com.alert( res.data.message,function(){
                    app.globalData.adminurl = app.com.getUrlArgs();
                    if( res.data.errno == 2 ){
                        wx.navigateTo({
                            url : res.data.data.url
                        });
                    }
                });
            });
            
        },self);
        wx.setNavigationBarTitle({
          title: '卡券管理',
        });
        
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '卡券管理',
            path: '',
        }
    },
    tab(e) {
        var self = this;
        var index = e.detail.index;

        if( self.data.ising || index == self.data.page.pdata.type ) return false;
        self.data.ising = true;
        
        self.setData({
            datalist : [],
            'page.pdata.type' : index,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
            wx.pageScrollTo({
                scrollTop: 0
            });
        });
        self.data.ising = false;

    },
    onReachBottom : function(){
        var self = this;

        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    deletegoods(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        app.com.confirm('确定删除此卡券吗?',function(){
            app.com.http('admin','POST',{op:'deletecard',id:id},0,true,'','',function(res){
                if (!res.data.errno) {

                    app.com.toast(res.data.message,'success',function(){
                        for (let i = 0,il=self.data.datalist; i < il.length; i++) {
                            if( il[i].id == id ){
                                il.splice(i,1);
                                self.setData({
                                    datalist : il
                                });
                            }
                        }
                    })

                }else{
                    app.com.toast( res.data.message );
                    if( res.data.errno == 2 ){
                        wx.navigateTo({
                            url : res.data.data.url
                        });
                    }
                }
            });
        });
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    }
}))